package com.example.xiaodu.yuejian12.Model;

import com.example.xiaodu.yuejian12.Bean.ActivityBean;
import com.example.xiaodu.yuejian12.Bean.ClubBean;
import com.example.xiaodu.yuejian12.Bean.GameBean;
import com.example.xiaodu.yuejian12.Bean.LoginBean;
import com.example.xiaodu.yuejian12.Bean.MasterBean;
import com.example.xiaodu.yuejian12.Bean.RegisterBean;
import com.example.xiaodu.yuejian12.Bean.ShowBean;
import com.example.xiaodu.yuejian12.Bean.TrainBean;
import com.example.xiaodu.yuejian12.InterFace.RetrofitService;
import com.example.xiaodu.yuejian12.Listener.RetrofitListener;
import com.example.xiaodu.yuejian12.MeIP;
import com.google.gson.Gson;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static android.os.Build.ID;

/**
 * Created by xiaodu on 2018/9/27.
 */

public class UserModel {
    MeIP ip=new MeIP();
    String meIP=ip.getIP();

    public void userLogin(String username,String password,final RetrofitListener listener){
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(meIP+"/yuejian/public/index.php/api/login/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        RetrofitService service=retrofit.create(RetrofitService.class);
        Call<LoginBean>call=service.login(username, password);
        call.enqueue(new Callback<LoginBean>() {
            @Override
            public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
                listener.onSuccess(response.body());
            }

            @Override
            public void onFailure(Call<LoginBean> call, Throwable t) {
                 listener.onFail(t.toString());
            }
        });
    }


    public void userRegister(String username,String password,String phone,final RetrofitListener listener){
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(meIP+"/yuejian/public/index.php/api/register/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        RetrofitService service=retrofit.create(RetrofitService.class);
        Call<RegisterBean>call=service.register(username,password,phone);
        call.enqueue(new Callback<RegisterBean>() {
            @Override
            public void onResponse(Call<RegisterBean> call, Response<RegisterBean> response) {
                listener.onSuccess(response.body());
            }

            @Override
            public void onFailure(Call<RegisterBean> call, Throwable t) {
                listener.onFail(t.toString());

            }
        });

    }

        public void getAllActivity(final RetrofitListener listener){
            Retrofit retrofit=new Retrofit.Builder()
                    .baseUrl(meIP+"/yuejian/public/index.php/api/activity/")
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
            RetrofitService service=retrofit.create(RetrofitService.class);
            Call<List<ActivityBean>> call=service.getAllActivity();
            call.enqueue(new Callback<List<ActivityBean>>() {
                @Override
                public void onResponse(Call<List<ActivityBean>> call, Response<List<ActivityBean>> response) {
                    listener.onSuccess(response.body());
                }

                @Override
                public void onFailure(Call<List<ActivityBean>> call, Throwable t) {
                      listener.onFail(t.toString());
                }
            });
        }

    public void getAllGame(final RetrofitListener listener){
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(meIP+"/yuejian/public/index.php/api/game/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        RetrofitService service=retrofit.create(RetrofitService.class);
        Call<List<GameBean>> call=service.getAllGame();
        call.enqueue(new Callback<List<GameBean>>() {
            @Override
            public void onResponse(Call<List<GameBean>> call, Response<List<GameBean>> response) {
                listener.onSuccess(response.body());
            }

            @Override
            public void onFailure(Call<List<GameBean>> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }

        public void getAllClub(final RetrofitListener listener){
            Retrofit retrofit=new Retrofit.Builder()
                    .baseUrl(meIP+"/yuejian/public/index.php/api/club/")
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
            RetrofitService service=retrofit.create(RetrofitService.class);
            Call<List<ClubBean>> call=service.getAllClub();
            call.enqueue(new Callback<List<ClubBean>>() {
                @Override
                public void onResponse(Call<List<ClubBean>> call, Response<List<ClubBean>> response) {
                    listener.onSuccess(response.body());
                }

                @Override
                public void onFailure(Call<List<ClubBean>> call, Throwable t) {
                    listener.onFail(t.toString());

                }
            });

        }

    public void getAllMaster(final RetrofitListener listener){
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(meIP+"/yuejian/public/index.php/api/master/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        RetrofitService service=retrofit.create(RetrofitService.class);
        Call<List<MasterBean>> call=service.getAllMaster();
        call.enqueue(new Callback<List<MasterBean>>() {
            @Override
            public void onResponse(Call<List<MasterBean>> call, Response<List<MasterBean>> response) {
                listener.onSuccess(response.body());
            }

            @Override
            public void onFailure(Call<List<MasterBean>> call, Throwable t) {
                listener.onFail(t.toString());

            }
        });

    }

    public void getAllTrain(final RetrofitListener listener){
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(meIP+"/yuejian/public/index.php/api/train/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        RetrofitService service=retrofit.create(RetrofitService.class);
        Call<List<TrainBean>> call=service.getAllTrain();
        call.enqueue(new Callback<List<TrainBean>>() {
            @Override
            public void onResponse(Call<List<TrainBean>> call, Response<List<TrainBean>> response) {
                listener.onSuccess(response.body());
            }

            @Override
            public void onFailure(Call<List<TrainBean>> call, Throwable t) {
                 listener.onFail(t.toString());
            }
        });
    }



        public void getAllShow(final RetrofitListener listener){
            Retrofit retrofit=new Retrofit.Builder()
                    .baseUrl(meIP+"/yuejian/public/index.php/api/show/")
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
            RetrofitService service=retrofit.create(RetrofitService.class);
            Call<List<ShowBean>> call=service.getAllShow();
            call.enqueue(new Callback<List<ShowBean>>() {
                @Override
                public void onResponse(Call<List<ShowBean>> call, Response<List<ShowBean>> response) {
                    listener.onSuccess(response.body());
                }

                @Override
                public void onFailure(Call<List<ShowBean>> call, Throwable t) {
                    listener.onFail(t.toString());

                }
            });
        }

        public void getActivityDetail(int activityID,final RetrofitListener listener){
            Retrofit retrofit=new Retrofit.Builder()
                    .baseUrl(meIP+"/yuejian/public/index.php/api/activity/")
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
            RetrofitService service=retrofit.create(RetrofitService.class);
            Call<ActivityBean>call=service.getActivityByID(activityID);
            call.enqueue(new Callback<ActivityBean>() {
                @Override
                public void onResponse(Call<ActivityBean> call, Response<ActivityBean> response) {
                    listener.onSuccess(response.body());
                }

                @Override
                public void onFailure(Call<ActivityBean> call, Throwable t) {
                      listener.onFail(t.toString());
                }
            });
        }

    public void getShowDetail(int showID,final RetrofitListener listener){
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(meIP+"/yuejian/public/index.php/api/show/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        RetrofitService service=retrofit.create(RetrofitService.class);
        Call<ShowBean>call=service.getShowID(showID);
        call.enqueue(new Callback<ShowBean>() {
            @Override
            public void onResponse(Call<ShowBean> call, Response<ShowBean> response) {
                listener.onSuccess(response.body());
            }

            @Override
            public void onFailure(Call<ShowBean> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }

    public void getMasterDetail(int masterID,final RetrofitListener listener ){
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(meIP+"/yuejian/public/index.php/api/master/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        RetrofitService service=retrofit.create(RetrofitService.class);
        Call<MasterBean>call=service.getMasterByID(masterID);
        call.enqueue(new Callback<MasterBean>() {
            @Override
            public void onResponse(Call<MasterBean> call, Response<MasterBean> response) {
                listener.onSuccess(response.body());
            }

            @Override
            public void onFailure(Call<MasterBean> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }

    public void getGameDetail(int gameID,final RetrofitListener listener){
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(meIP+"/yuejian/public/index.php/api/game/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        RetrofitService service=retrofit.create(RetrofitService.class);
        Call<GameBean>call=service.getGameByID(gameID);
        call.enqueue(new Callback<GameBean>() {
            @Override
            public void onResponse(Call<GameBean> call, Response<GameBean> response) {
                listener.onSuccess(response.body());
            }

            @Override
            public void onFailure(Call<GameBean> call, Throwable t) {
                 listener.onFail(t.toString());
            }
        });
    }

    public void getClubDetail(int clubID,final RetrofitListener listener){
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(meIP+"/yuejian/public/index.php/api/club/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        RetrofitService service=retrofit.create(RetrofitService.class);
        Call<ClubBean>call=service.getClubByID(clubID);
        call.enqueue(new Callback<ClubBean>() {
            @Override
            public void onResponse(Call<ClubBean> call, Response<ClubBean> response) {
                listener.onSuccess(response.body());
            }

            @Override
            public void onFailure(Call<ClubBean> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }

    public void getTrainDetail(int trainID,final RetrofitListener listener){
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(meIP+"/yuejian/public/index.php/api/train/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        RetrofitService service=retrofit.create(RetrofitService.class);
        Call<TrainBean>call=service.getTrainByID(trainID);
        call.enqueue(new Callback<TrainBean>() {
            @Override
            public void onResponse(Call<TrainBean> call, Response<TrainBean> response) {
                listener.onSuccess(response.body());
            }

            @Override
            public void onFailure(Call<TrainBean> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }
    public void signupActivity(int aid,int uid,String phone,String people,final RetrofitListener listener){
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(meIP+"/yuejian/public/index.php/api/signup/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        RetrofitService service=retrofit.create(RetrofitService.class);
        Call<LoginBean>call=service.signupActivity(aid, uid, phone, people);
        call.enqueue(new Callback<LoginBean>() {
            @Override
            public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
                listener.onSuccess(response.body());
            }

            @Override
            public void onFailure(Call<LoginBean> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }

    public void signupGame(int gid,int uid,String phone,final RetrofitListener listener){
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(meIP+"/yuejian/public/index.php/api/signup1/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        RetrofitService service=retrofit.create(RetrofitService.class);
        Call<LoginBean>call=service.signupGame(gid, uid, phone);
        call.enqueue(new Callback<LoginBean>() {
            @Override
            public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
                listener.onSuccess(response.body());
            }

            @Override
            public void onFailure(Call<LoginBean> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }

    public void signupClub(int cid,int uid,String phone,String email,final RetrofitListener listener){
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(meIP+"/yuejian/public/index.php/api/signup2/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        RetrofitService service=retrofit.create(RetrofitService.class);
        Call<LoginBean>call=service.signupClub(cid, uid, phone,email);
        call.enqueue(new Callback<LoginBean>() {
            @Override
            public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
                listener.onSuccess(response.body());
            }

            @Override
            public void onFailure(Call<LoginBean> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }

    public void signupTrain(int tid,int uid,String phone,String time,final RetrofitListener listener){
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(meIP+"/yuejian/public/index.php/api/signup3/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        RetrofitService service=retrofit.create(RetrofitService.class);
        Call<LoginBean>call=service.signupTrain(tid, uid, phone,time);
        call.enqueue(new Callback<LoginBean>() {
            @Override
            public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
                listener.onSuccess(response.body());
            }

            @Override
            public void onFailure(Call<LoginBean> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }


}
